import 'package:flutter/material.dart';
import 'package:xws/models/user/user_update_obj.dart';
import 'package:xws/res/icons.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';

/// 编辑性别
Future<void> editGender(BuildContext context) async {
  final String? gender = await push<String>(
    context,
    const _EditGender(),
    routeType: RouteType.fade,
  );

  if (gender == null) return;

  loading(context);

  final bool r = await updateInfo(UserUpdateObj(gender: gender));

  close(context);

  if (r) await us.updateBasicInfo();
}

class _EditGender extends StatelessWidget {
  const _EditGender({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => pop(context),
      child: Material(
        color: black(context).withOpacity(0.4),
        child: Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.symmetric(horizontal: 30),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <String>['男', '女'].map(
              (String v) {
                final bool select = us.info.gender == v;

                return AppBtn.noPadding(
                  height: 110,
                  width: 110,
                  radius: 55,
                  border: Border.all(color: white(context), width: 1),
                  onTap: () => pop(context, value: v),
                  color: select ? theme(context) : white(context),
                  child: Stack(
                    alignment: Alignment.center,
                    children: <Widget>[
                      Icon(
                        v == '男' ? IR.male : IR.fmale,
                        size: 40,
                        color: select ? white(context) : theme(context),
                      ),
                      Positioned(
                        bottom: 4,
                        child: Text(
                          v,
                          style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.bold,
                            color: select ? white(context) : black(context),
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ).toList(),
          ),
        ),
      ),
    );
  }
}
